<?php

    class itPersistentCacheTest extends itTestCase {
        
        protected $file;
        
        public function __construct() {
            $this->file = TEST_DATA.'/Individual/LibUtil/Cache/doesnotexist.php';
        }
        
        /**
         * @test
         */
        public function testStart() {
            if (is_file($this->file)) {
                unlink($this->file);
            }
        }
        
        /**
         * @depends testStart
         * @test
         */
        public function testCacheDoesNotExist() {
            $file = $this->file;
            $this->assertFalse(is_file($file));
            $cache = new itPersistentCache($file);
            $this->assertTrue($cache->virgin());
            $this->assertFalse(is_file($file));
            return $cache;
        }
        
        /**
         * @depends testCacheDoesNotExist
         * @test
         */
        public function testBadValue($cache) {
            try {
                $cache->set('abc', 'www');
                $this->fail();
            } catch(itException $e) {
                $this->assertType('itParameterException', $e);
            }

            return $cache;
        }
        
        /**
         * @depends testBadValue
         * @test
         */
        public function testGoodValue($cache) {
            $simpleRules = new itSimpleRules();
            $simpleRules->addFile('aaa')->addFile('bbb')->addFile('ccc');
            $cache->set('abc', $simpleRules);
            $this->assertTrue(is_file($this->file));
            $this->assertEquals($simpleRules, $cache->get('abc'));
            return;
        }
        
        /**
         * @depends testGoodValue
         * @test
         */
        public function testCacheExists() {
            $cache = new itPersistentCache($this->file);
            $this->assertFalse($cache->virgin());
            $expected = array('aaa', 'bbb', 'ccc');
            $fromCache = $cache->get('abc')->toPersistentCache();
            $this->assertEquals($expected, $fromCache);
            return $cache;
        }
        
        /**
         * @depends testCacheExists
         * @test
         */
        public function testClearCache($cache) {
            $cache->del('abc');
            $this->assertEquals(null, $cache->get('abc'));
            $this->assertTrue(is_file($this->file));
            $cache->clear();
            $this->assertFalse(is_file($this->file));
        }
        
    }
    
?>